package com.jd.fruit.model.admin.shop;

import java.util.List;

import com.jd.fruit.common.annotation.Table;
import com.jd.fruit.common.model.BaseModel;
import com.jd.fruit.common.util.StringUtil;

/**
 * 商品 对应属性
 * 
 * @author lishunfeng
 * 
 */
@SuppressWarnings("serial")
@Table("t_goods_attribute")
public class GoodsAttribute extends BaseModel<GoodsAttribute> {
	public static final GoodsAttribute dao = new GoodsAttribute();

	/**
	 * 保存商品SKU
	 * 
	 * @param role
	 */
	public void save(GoodsAttribute goodsAttr) {
		try {
			goodsAttr.set("id", StringUtil.getKey());
			goodsAttr.save();
		} catch (Exception e) {
			throw new RuntimeException("商品属性Sku保存异常！", e);
		}
	}
	
	/**
	 * 读取商品属性及对应值
	 * @param goodsId
	 * @return
	 */
	public List<GoodsAttribute> listByGoodsId(String goodsId) {
		String sql="SELECT tca.`attr_value`,tga.* FROM `t_goods_attribute` tga  JOIN t_attribute ta ON tga.`attr_id`=ta.`id`   JOIN `t_category_attribute` tca ON  tga.`cat_attr_id`=tca.`id`  WHERE tga.`goods_id`=?";
		return this.find(sql, goodsId);
	}
	
	/**
	 * 读取商品属性
	 * @param goodsId
	 * @return
	 */
	public List<GoodsAttribute> attrListByGoodsId(String goodsId) {
		String sql="SELECT ta.`attr_name`,tca.`attr_id` FROM `t_goods_attribute` tga JOIN t_attribute ta  ON tga.`attr_id`=ta.`id` JOIN `t_category_attribute` tca  ON  tga.`cat_attr_id`=tca.`id` WHERE tga.`goods_id`=? GROUP BY tca.`attr_id` ORDER BY ta.`attr_sort`";
		return this.find(sql, goodsId);
	}
	
	
	@Override
	public void initWhere() {
		// TODO Auto-generated method stub

	}
	
}
